<template>
  <div class="app-container">
    <basic-container v-show="showSearch">
      <common-search @handleQuery="handleQuery" @resetQuery="resetQuery">
        <el-form
          :model="queryParams"
          ref="queryForm"
          :inline="true"
          size="mini"
          label-width="128px"
        >
          <el-form-item label="主管海关" prop="mainPort">
            <el-select
              v-model.trim="queryParams.mainPort"
              placeholder="请选择主管海关"
              clearable
            >
              <el-option v-for="(item, index) in customsList" :key="index" :label="item.label" :value="item.value" />
            </el-select>
          </el-form-item>
          <el-form-item label="车辆海关编码" prop="veCustomsNo">
            <el-input
              v-model.trim="queryParams.veCustomsNo"
              placeholder="请输入车辆海关编码"
              clearable
              @keyup.enter.native="handleQuery"
            />
          </el-form-item>
          <el-form-item label="国内车牌" prop="domesticLisenceNo">
            <el-input
              v-model.trim="queryParams.domesticLisenceNo"
              placeholder="请输入国内车牌"
              clearable
              @keyup.enter.native="handleQuery"
            />
          </el-form-item>
          <el-form-item label="外籍车牌" prop="foreignLicense">
            <el-input
              v-model.trim="queryParams.foreignLicense"
              placeholder="请输入外籍车牌"
              clearable
              @keyup.enter.native="handleQuery"
            />
          </el-form-item>
          <el-form-item label="自重（整备质量）" prop="selfWt">
            <el-input
              v-model.trim="queryParams.selfWt"
              placeholder="请输入自重（整备质量）"
              clearable
              @keyup.enter.native="handleQuery"
            />
          </el-form-item>
          <el-form-item label="申报状态" prop="declStatus">
            <el-select
                v-model.trim="queryParams.declStatus"
                placeholder="请选择申报状态"
              >
                <el-option
                  v-for="(item, index) in dict.type.sw_vehicle_decl_status"
                  :key="index"
                  :label="item.label"
                  :value="item.value"
                />
              </el-select>
          </el-form-item>
        </el-form>
      </common-search>
    </basic-container>

    <basic-container>
      <el-row :gutter="10" class="mb8">
        <el-col :span="1.5">
          <el-button
            type="primary"
            plain
            icon="el-icon-plus"
            size="mini"
            @click="handleAddEdit('add')"
            v-hasPermi="['bt:swRoadVeRecord:add']"
            >新增</el-button
          >
        </el-col>
        <el-col :span="1.5">
          <el-button
            type="danger"
            plain
            icon="el-icon-delete"
            size="mini"
            :disabled="multiple"
            @click="handleDelete"
            v-hasPermi="['bt:swRoadVeRecord:remove']"
            >删除</el-button
          >
        </el-col>
        <el-col :span="1.5">
          <el-button
            type="warning"
            plain
            icon="el-icon-upload"
            size="mini"
            @click="handleDecl"
            v-hasPermi="['bt:swRoadVeRecord:decl']"
          >申报
          </el-button>
        </el-col>
        <el-col :span="1.5">
          <el-button
            type="danger"
            plain
            icon="el-icon-upload"
            size="mini"
            :disabled="single"
            @click="handleAddEdit('declModify')"
            v-hasPermi="['bt:swRoadVeRecord:decl']"
          >变更申报
          </el-button>
        </el-col>
        <!-- <el-col :span="1.5">
          <el-button
            type="warning"
            plain
            icon="el-icon-download"
            size="mini"
            :loading="exportLoading"
            @click="handleExport"
            v-hasPermi="['bt:swRoadVeRecord:export']"
          >导出</el-button>
        </el-col>
        -->
        <right-toolbar
          :showSearch.sync="showSearch"
          @queryTable="getList"
        ></right-toolbar>
      </el-row>

      <!-- 列表 -->
      <basic-table
        ref="tableList"
        :columns="columns"
        :tableData="tableData"
        :total="pageParams.total"
        @handleSizeChange="handleSizeChange"
        @handleCurrentChange="handleCurrentChange"
        :loading="loading"
        :needCheckbox="true"
        @selectionChange="handleSelectionChange"
      >
        <!-- 关区代码 -->
        <template #mainPort="scope">
          <dict-value
            :list="customsList"
            :dictCode="scope.row.mainPort"
          ></dict-value>
        </template>
        <!-- 注册地 -->
        <template #veRegPlace="scope">
          <dict-tag
            :options="dict.type.sw_vehicle_reg_place"
            :value="scope.row.veRegPlace"
          ></dict-tag>
        </template>
        <!-- 国籍 -->
        <template #nationality="scope">
          <dict-tag
            :options="dict.type.mft_route_countrys"
            :value="scope.row.nationality"
          ></dict-tag>
        </template>
        <!-- 申报状态 -->
        <template #declStatus="scope">
          <dict-tag
            :options="dict.type.sw_vehicle_decl_status"
            :value="scope.row.declStatus"
          ></dict-tag>
        </template>
        <!-- 回执状态 -->
        <template #rcptStatus="scope">
          <dict-tag
            :options="dict.type.sw_vehicle_rcpt_status"
            :value="scope.row.rcptStatus"
          ></dict-tag>
        </template>
        <template #operate="scope">
          <div class="option-menu">
            <el-button
              type="text"
              size="mini"
              class="table-edit"
              @click="handleAddEdit('edit', scope.row)"
              v-hasPermi="['bt:swRoadVeRecord:edit']"
            >
              编辑
            </el-button>
            <el-button
              type="text"
              size="mini"
              class="table-edit"
              @click="handleDelete(scope.row)"
              v-hasPermi="['bt:swRoadVeRecord:remove']"
            >
              删除
            </el-button>
            <el-button
              type="text"
              size="mini"
              class="table-edit"
              @click="handleDetail(scope.row)"
            >
              详情
            </el-button>
          </div>
        </template>
      </basic-table>
    </basic-container>

    <!-- 新增、编辑 -->
    <add-edit
      ref="addEdit"
      :dialogType="dialogType"
      :rowData="rowData"
      :dict="dictProp"
      :customsList="customsList"
      @handleQuery="handleQuery"
    />

    <!-- 详情 -->
    <detail-dialog
      ref="detail"
      :detailId="detailId"
      :customsList="customsList"
      :dict="dictProp"
    ></detail-dialog>
  </div>
</template>

<script>
import {
  listSwRoadVeRecord,
  delSwRoadVeRecord,
  exportSwRoadVeRecord,
  declSwRoadVeRecord,
} from "@/api/swdecl/swRoadVeRecord";
import addEdit from "./components/addEdit";
import detailDialog from "./components/detailDialog";
import {getCustomsList} from "@/api/bt/bmhs";
export default {
  name: "SwRoadVeRecord",
  dicts: ["sw_vehicle_limit_num", "mft_route_countrys", "sw_vehicle_class_flag", "sw_vehicle_convey_qua", "sw_vehicle_property", "sw_vehicle_reg_place", "sw_vehicle_type", "sw_vehicle_decl_status", "sw_vehicle_rcpt_status"],
  components: {
    addEdit,
    detailDialog,
  },
  data() {
    return {
      // 显示搜索条件
      showSearch: true,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        mainPort: null,
        veCustomsNo: null,
        domesticLisenceNo: null,
        foreignLicense: null,
        selfWt: null,
        declStatus: null,
      },
      // 遮罩层
      loading: true,
      // 列表数据
      tableData: [],
      columns: [
        {
          prop: "domesticLisenceNo",
          label: "国内车牌",
          align: "center",
          minWidth: 100
        },

        // {
        //   prop: "domesticLicenseColor",
        //   label: "国内车牌颜色",
        //   align: "center",
        //   minWidth: 100
        // },

        {
          prop: "foreignLicense",
          label: "外籍车牌",
          align: "center",
          minWidth: 120
        },
        {
          prop: "veFrameNo",
          label: "车架号",
          align: "center",
          minWidth: 140
        },
        {
          prop: "mainPort",
          label: "主管海关",
          align: "center",
          slot: true,
          minWidth: 140
        },
        {
          prop: "nationality",
          label: "国籍",
          align: "center",
          minWidth: 100,
          slot: true
        },
        {
          prop: "coCode",
          label: "企业代码",
          align: "center",
          minWidth: 120
        },
        // {
        //   prop: "veCardNo",
        //   label: "车辆行驶证编号",
        //   align: "center",
        //   minWidth: 120
        // },

        {
          prop: "veOwnerName",
          label: "车主名称",
          align: "center",
          minWidth: 120
        },

        {
          prop: "veOwnerNo",
          label: "车主证件号码",
          align: "center",
          minWidth: 120
        },

        // {
        //   prop: "ownerInsideAddr",
        //   label: "车主境内联系地址",
        //   align: "center",
        //   minWidth: 140
        // },

        {
          prop: "ownerInsideTel",
          label: "车主境内联系电话",
          align: "center",
          minWidth: 130
        },
        {
          prop: "declStatus",
          label: "申报状态",
          align: "center",
          fixed: "right",
          minWidth: 100,
          slot: true
        },

        {
          prop: "rcptStatus",
          label: "回执状态",
          align: "center",
          fixed: "right",
          slot: true,
        },

        {
          prop: "proposer",
          label: "申请人",
          align: "center",
        },

        {
          prop: "proposeTime",
          label: "申请时间",
          // label: "申请时间（申报动作的时候上去）",
          align: "center",
          width: 140
        },
        {
          prop: "operate",
          label: "操作",
          slot: true,
          width: 140,
          fixed: "right",
        },
      ],
      pageParams: {
        total: 0,
      },
      rowData: {},
      detailId: "",
      dialogType: "",
      // 导出遮罩层
      exportLoading: false,
      // 选中数组
      ids: [],
      selectRow: {},
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      //详情显示的时候，表单编辑禁用
      isViewDisabled: false,

      // 运管备案查询表格数据
      swRoadVeRecordList: [],
      //海关关区信息列表
      customsList: [],
    };
  },
  computed: {
    dictProp() {
      //字典list，为避免子组件重复调接口获取，用computed计算传值
      return {
        type: this.dict.type,
      };
    },
  },
  created() {
    this.getList();
    this.loadCustomsList();
  },
  methods: {
    /** 查询运管备案查询列表 */
    getList() {
      this.loading = true;
      listSwRoadVeRecord(this.queryParams).then((response) => {
        this.tableData = response.rows;
        this.pageParams.total = response.total;
        this.loading = false;
      });
    },
    /* 加载关区信息列表*/
    loadCustomsList() {
      getCustomsList().then((response) => {
        this.customsList = response;
      });
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.$refs.tableList.pageParams.currentPage = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 分页
    handleSizeChange(val) {
      this.queryParams.pageSize = val.pageSize;
      this.getList();
    },
    handleCurrentChange(val) {
      this.queryParams.pageNum = val.currentPage;
      this.getList();
    },
    // 新增/编辑
    handleAddEdit(type, row) {
      this.dialogType = type;
      if (row) {
        this.rowData = JSON.parse(JSON.stringify(row));
      }
      if(this.ids.length === 1) {
        this.rowData = JSON.parse(JSON.stringify(this.selectRow));
      }
      this.$refs.addEdit.open();
    },
    // 详情
    handleDetail(row) {
      this.detailId = row.id;
      this.$nextTick(() => {
        this.$refs.detail.open();
      });
    },
    //申报
    handleDecl(row) {
      const billCodes = row.id || this.ids;
      if (billCodes == undefined || billCodes == null || billCodes == "") {
        this.$modal.alertError("请先选择要申报的备案数据！");
        return false;
      }
      this.$modal.confirm('是否确认申报业务编号为"' + billCodes + '"的数据项？').then(function () {
        return declSwRoadVeRecord(billCodes);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("申报成功");
      }).catch(() => {
      });
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map((item) => item.id);
      if(selection.length === 1) {
        this.selectRow = selection[0];
      } else {
        this.selectRow = {};
      }
      this.single = selection.length !== 1;
      this.multiple = !selection.length;
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$modal
        .confirm('是否确认删除运管备案查询编号为"' + ids + '"的数据项？')
        .then(function () {
          return delSwRoadVeRecord(ids);
        })
        .then(() => {
          this.getList();
          this.$modal.msgSuccess("删除成功");
        })
        .catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      const queryParams = this.queryParams;
      this.$modal
        .confirm("是否确认导出所有运管备案查询数据项？")
        .then(() => {
          this.exportLoading = true;
          return exportSwRoadVeRecord(queryParams);
        })
        .then((response) => {
          this.download(response.msg);
          this.exportLoading = false;
        })
        .catch(() => {});
    },
  },
};
</script>



<style type="text/css">
.el-form-item .el-select {
  width: 100%;
}
</style>
